﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;

namespace Send2Sharepoint.Forms
{
    public class MultiChoiceControl : InputControl
    {
        public MultiChoiceControl(XmlNode element) : base(element)
        {
            var attributeCollection = element.Attributes;

            var choicesNode = (from xmlNode in element.ChildNodes.Cast<XmlNode>()
                               where xmlNode.Name == "CHOICES"
                               select xmlNode).SingleOrDefault();
            var defaultNode = (from xmlNode in element.ChildNodes.Cast<XmlNode>()
                               where xmlNode.Name == "Default"
                               select xmlNode).SingleOrDefault();


            IEnumerable<string> choices = from choice in choicesNode.ChildNodes.Cast<XmlNode>()
                                  select choice.InnerText;
            string defaultValue = defaultNode != null ? defaultNode.InnerText : "";
            var name = attributeCollection["Name"].Value;
            const int x = 10;
            var y = 10;
            Field = new GroupBox
            {
                Name = "controlGroup_Check",
                Size = new Size(250, 22)
            };
            ((GroupBox)Field).FlatStyle = FlatStyle.System;
            foreach (var choice in choices)
            {
                var checkBox = new CheckBox
                {
                    Location = new Point(x, y),
                    Text = choice,
                    Name = name + "_" + choice
                };
                Field.Controls.Add(checkBox);
                checkBox.AutoSize = true;
                y += checkBox.Size.Height;
                Field.Height = y + 5;
                if (choice == defaultValue) checkBox.Checked = true;
            }
        }
    }
}